import file from '@system.file';

export default {
    data: {
        firststart: false,
        userDB: [],
        status: false,
        user: "",
        path: ""
    },
    onCreate() {
        console.info('AceApplication onCreate');
        file.access({
            uri: "internal://app/firstStart",
            fail: () => {
                getApp().firstStart = true;
                file.writeText({
                    uri: "internal://app/firstStart",
                    text: "2022110106",
                    fail: (data, code) => console.log(data, code)
                });
            }
        });
    },
    onDestroy() {
        console.info('AceApplication onDestroy');
    },
    isLoggedIn() {
        return this.status;
    },
    getUser() {
        return this.user;
    },
    login(u, p) {
        for (let i = 0; i < this.userDB.length; ++i) {
            if (this.userDB[i].user === u && this.userDB[i].password === p) {
                console.log("Login success");
                this.status = true;
                this.user = u;
                return true;
            }
        }
        return false;
    },
    register(u, p) {
        this.userDB.push({ "user": u, "password": p });
        console.log("New user: ", u, p);
    }
};
